<template>
  <el-table :data="tableData" border style="width: 100%" size='mini' :span-method="objectSpanMethod" @selection-change="selectSmsList" >
    <el-table-column prop="getHomeDate" label="日期" width="120"></el-table-column>
    <el-table-column prop="getHomeTime" label="AM/PM" width="80"></el-table-column>
    <el-table-column type="selection" width="55" :selectable='selectable'></el-table-column>
    <el-table-column prop="fullName" label="房号" width="180"></el-table-column>
    <el-table-column prop="customer" label="业主" width="180">
      <template slot-scope="scope">
        <span>{{scope.row.customer}} {{scope.row.phone}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="serviceTimes" label="所需时间" width="80">
      <template slot-scope="scope">
        <span>{{scope.row.serviceTimes}}分钟</span>
      </template>
    </el-table-column>
    <el-table-column prop="remark" label="备注" ></el-table-column>
    <el-table-column prop="serviceTime" label="服务时间" width="120"></el-table-column>
    <el-table-column prop="adminName" label="服务管家" width="80"></el-table-column>
    <el-table-column label="操作" width="180">
      <template slot-scope="scope">
        <el-button v-if="!scope.row.adminName" @click="setServerTime('setEdit',scope.row)" type="text" size="small">分配管家</el-button>
        <el-button v-if="scope.row.adminName" @click="setServerTime('editAll',scope.row)" type="text" size="small">更改服务时间</el-button>
        <span style="margin-left:15px;" v-if="scope.row.smsSend==1">短信已发送</span>
      </template>
    </el-table-column>
  </el-table>
</template>
<script>
import { request } from "../../assets/request.js"; 
export default {
  methods: {
    handleClick(row) {
      console.log(row);
    }
  },
  data() {
    return {
      tableData: [],
      mergeArr:[],
      mergeArr2:[],
      selobj:{}
    };
  },
  created(){
    this.$root.$on('selOrder',(obj)=>{
      this.selobj=obj
      this.selectList()
    })
    this.$root.$on('orderAllList',(type)=>{
      // console.log(type)
      this.selectList()
    })

  },
  methods:{
    setServerTime(type,row){ //更改服务时间
      this.$root.$emit('setServerTime',type,row)
    },
    selectable(row,index){
      console.log(row)
      if(row.status==3){
        return 1;
      }else{
        return 0;
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      //合并单元格
      //
      if (columnIndex === 0) {  //合并天单元格
          if(this.mergeArr[rowIndex]){
              return {
                  rowspan:this.mergeArr[rowIndex],
                  colspan:1
              }
          }else{
              return {
                  rowspan: 0,
                  colspan: 0
              }
          }
      }
      if (columnIndex === 1) {  //合并上午下午单元格
          if(this.mergeArr2[rowIndex]){
              return {
                  rowspan:this.mergeArr2[rowIndex],
                  colspan:1
              }
          }else{
              return {
                  rowspan: 0,
                  colspan: 0
              }
          }
      }
    },
    selectSmsList(val){//表格多选框选中
      this.$root.$emit('selectSmsList',val)
    },
    selectList(){
      let that=this
      let url = "order/distribution";
      let obj=this.selobj
      request.ajaxGet({
          url,
          that,
          params:obj,
          fn(res){
              // console.log(res)
              let arr=[]
              let mergeArr=[]
              let mergeArr2=[]
              for(let i=0,len=res.length;i<len;i++){
                mergeArr.push(res[i].size)
                for(let j in res[i].orderDistributionTimeList){
                  for(let k in res[i].orderDistributionTimeList[j].orderDistributionList){
                    if(k=='0'){//合并同一天上午下午的单元格
                      mergeArr2.push(res[i].orderDistributionTimeList[j].size)
                    }else{
                      mergeArr2.push(0)
                    }
                    res[i].orderDistributionTimeList[j].orderDistributionList[k].getHomeDate=res[i].getHomeDate
                    res[i].orderDistributionTimeList[j].getHomeTime= res[i].orderDistributionTimeList[j].getHomeTime==2?'下午': res[i].orderDistributionTimeList[j].getHomeTime==1?'上午':'全天'
                    res[i].orderDistributionTimeList[j].orderDistributionList[k].getHomeTime=res[i].orderDistributionTimeList[j].getHomeTime
                    arr.push(res[i].orderDistributionTimeList[j].orderDistributionList[k])
                  }
                }
              }
              let mergeArr1=[]
              for(let i=0,len=mergeArr.length;i<len;i++){
                for(let j=0;j<mergeArr[i];j++){
                  if(j=='0'){//合并同一天上午下午的单元格
                    mergeArr1.push(mergeArr[i])
                  }else{
                    mergeArr1.push(0)
                  }
                }
              }
              that.mergeArr=mergeArr1
              that.mergeArr2=mergeArr2
              that.tableData=arr
          }
      })
    }
  }
};
</script>
